package com.huawang.mdesign.model.service.util.uaf;

import com.huawang.core.definition.mof.UMLDefinitionConstants;
import com.huawang.core.semantics.entity.MofPropertyInfo;
import org.jetbrains.annotations.Nullable;

import java.util.*;

public class PropertyUtils {

    private PropertyUtils() {}

    /**
     * 通过实例id和属性名称获得属性值
     *
     */
    @Nullable
    public static Object getInstancePropertyValueByPropertyMap(String instanceId, String propertyKey, Map<String, Map<String, MofPropertyInfo>> instancesPropertyInfoMap) {
        Map<String, MofPropertyInfo> instancePropertyInfoMap = instancesPropertyInfoMap.get(instanceId);
        if (instancePropertyInfoMap != null) {
            return getValueFromMofProperty(instancePropertyInfoMap, propertyKey);
        }
        return null;
    }

    /**
     * 从属性信息中获取指定属性的值
     *
     */
    @Nullable
    public static Object getValueFromMofProperty(Map<String, MofPropertyInfo> mofPropertyInfoMap, String propertyKey) {
        MofPropertyInfo mofPropertyInfo = mofPropertyInfoMap.get(propertyKey);
        if (mofPropertyInfo != null) {
            return mofPropertyInfo.getValues();
        }
        return null;
    }

    public static boolean getNullable(Integer lower, String propertyKey) {
        boolean nullable = false;
        if (lower != null && lower == 0) {
            nullable = !Objects.equals(propertyKey, UMLDefinitionConstants.Element_owner);
        } else {
            List<String> specificAttributes = Arrays.asList(UMLDefinitionConstants.CallBehaviorAction_behavior,
                    UMLDefinitionConstants.CallOperationAction_operation,
                    UMLDefinitionConstants.CreateObjectAction_classifier,
                    UMLDefinitionConstants.ReadExtentAction_classifier,
                    UMLDefinitionConstants.StructuralFeatureAction_structuralFeature,
                    UMLDefinitionConstants.SendSignalAction_signal);
            if (specificAttributes.contains(propertyKey)) {
                nullable = true;
            }
        }

        return nullable;
    }
}
